// pages/zdPark/zdPark.js
var util = require('../../utils/request.js');
var globalData = getApp().globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //订单信息
    orderInfo: {},
    noOrder: true,
    cardId: '',
    card: [],
    current: '0',
    animation: ''
  },
  changeSelect: function (e) {
    var that = this
    this.setData({
      current: e.detail.current
    })
    console.log(this.data.card.length)
    if (that.data.card[e.detail.current].ID != null) {
      this.setData({
        cardId: that.data.card[e.detail.current].licensePlateId
      })
      this.getOrder();
    } else {
      this.setData({
        noOrder: true
      })
    }
    console.log(this.data.current);
  },
  //去支付
  goPay: function () {
    var that = this
    util.req('/order/finish', { sn: this.data.orderInfo.sn, memberId: globalData.memberId, parkType: this.data.orderInfo.parkType }, function (success) {
      console.log(success);
      if (success.resultCode == 200) {
        wx.redirectTo({
          url: '../payOrder/payOrder?sn=' + that.data.orderInfo.sn,
        })
      } else if (success.resultCode == 400) {
        wx.showToast({
          title: success.resultMsg,
          icon: 'none'
        })
        setTimeout(function() {
          wx.navigateTo({
            url: '../order/order?to=2',
          })
        }, 800)
      } else {
        wx.showToast({
          title: '结束订单错误！',
          icon: 'none'
        })
      }
    })

  },
  //刷新缴费信息
  refreshOrder: function () {
    var that = this;
    this.setData({
      animation: true,
    })
    setTimeout(function () {
      that.setData({
        animation: '',
      })
    }, 1000)
    this.getOrder();
  },
  //获取实时缴费信息
  getOrder: function () {
    var that = this
    console.log(this.data.card);
    var name = that.data.card[that.data.current].number;
    // console.log(name);
    util.req('/order/refreshParkingOrder', { number: name }, function (success) {
      console.log(success)
      if (success.resultCode == 200) {
        var orderInfo = {
          finishDate: util.formatTime(success.resultContent.finishDate),
          orderDuration: util.formatSeconds(success.resultContent.orderDuration),
          business: success.resultContent.business,
          useDate: util.formatTime(success.resultContent.useDate),
          card: success.resultContent.number,
          amount: Math.floor(success.resultContent.amount * 100) / 100,
          parkType: success.resultContent.parkType,
          sn: success.resultContent.sn
        }
        that.setData({
          orderInfo: orderInfo,
          noOrder: false
        })
      } else {
        that.setData({
          noOrder: true
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var cardMsg = wx.getStorageSync('cardMessage');
    if(cardMsg.card != null && cardMsg.card != undefined) {
      var card = {
        number: cardMsg.card,
        type: cardMsg.cardType,
        name: cardMsg.card.slice(0, 2),
        ID: cardMsg.card.slice(2)
      }
      var cardArr = [];
      cardArr.push(card);
      console.log(cardArr);
      this.setData({
        card: cardArr
      })
      console.log(this.data.card);
      this.getOrder();
    } else {
      wx.showToast({
        title: '未查找到车牌',
        icon: 'none'
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})